﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace Microsoft.ProductivityHub.SilverlightProductCarousel
{
	public partial class CarouselItem : UserControl
	{

        public Boolean IsActive = false;
        public Boolean IsVisible = false;
        public int Index = 999;
        public ProductItem Data = null;
        public String Title = "";
        public String Description = "";

        // Hold Values for Current Position
        public Double Position = 0;
        public Double Top = 0;
        public Double Alpha = 1;
        public Double Scale = 1;
        public int Zindex = 0;

        // Hold Values for Target Positions
        public Double NewPosition = 0;
        public Double NewTop = 0;
        public Double NewAlpha = 1;
        public Double NewScale = 1;
        public int NewZindex = 0;

        // --------------------------------------------------------------------------------------
        // INITIALIZE COMPONENT
        // --------------------------------------------------------------------------------------

		public CarouselItem()
		{
			// Required to initialize variables
            InitializeComponent();  
		}




        // --------------------------------------------------------------------------------------
        // CUSTOM EVENT DELEGATES
        // --------------------------------------------------------------------------------------

        public delegate void ClickItemEvent(ProductItem itemClicked);
        public event ClickItemEvent ClickItem;

        public delegate void ShuffleToItemEvent(int index);
        public event ShuffleToItemEvent ShuffleToItem;

        public delegate void ShowTitleEvent(int index);
        public event ShowTitleEvent ShowTitle;


        // --------------------------------------------------------------------------------------
        // LOAD THUMBNAIL IMAGE
        // --------------------------------------------------------------------------------------
        //
        // These functions handle the loading of the thumbnail image

        public void LoadThumbnail(String NewUri)
        {
            LoadingAnimation.Visibility = Visibility.Visible;
            LoadingLoop.Begin();

            Thumbnail.Source = new BitmapImage(new Uri(NewUri, UriKind.RelativeOrAbsolute));
            Thumbnail.ImageOpened += new EventHandler<RoutedEventArgs>(ImageOpenedEvent);
        }

        private void ImageOpenedEvent(object sender, RoutedEventArgs e)
        {
            LoadingAnimation.Visibility = Visibility.Collapsed;
            Thumbnail.ImageOpened -= new EventHandler<RoutedEventArgs>(ImageOpenedEvent);
        }

        
        public void LoadReflection(String NewUri)
        {
            Reflection.Source = new BitmapImage(new Uri(NewUri, UriKind.RelativeOrAbsolute));
        }
        



        // --------------------------------------------------------------------------------------
        // MOUSE EVENT HANDLERS
        // --------------------------------------------------------------------------------------


        private void MouseEnterEvent(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (IsActive == true)
            {
                ShowTitle.Invoke(Index);
            }
        }

        private void MouseLeaveEvent(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (IsActive == true)
            {
                //_HideTitle.Invoke();
            }
        }

        private void MouseLeftButtonUpEvent(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (IsActive == true)
            {
                ClickItem.Invoke(Data);
            }
            if (IsVisible == true)
            {
                ShuffleToItem.Invoke(Index);
            }
        }









// -----------------------------------------------
// END CLASS
	}
}